﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Collector.Device
{
    public class PulseCommonHelper
    {
        public class Display
        {
            private string message;
            private LogMsgType logLevel = LogMsgType.INFO;
            private object userData;

            public DisplayMessageBox displayMsgBox = new DisplayMessageBox();
            public DisplayResultItemBox displayRltBox = new DisplayResultItemBox();

            public enum DisplayType
            {
                // UI message
                eDISPLAY_TYPE_MSG,
                eDISPLAY_TYPE_ITEM,
                eDISPLAY_TYPE_USER
            }

            public class DisplayMessageBox
            {
                public string message;
                public LogMsgType type;
            }

            public class DisplayResultItemBox
            {
                public string itemName;
                public string itemInfo;
                public bool result;
            }

            public string Message
            {
                set { message = value; }
                get { return message; }
            }

            public LogMsgType LogLevel
            {
                set { logLevel = value; }
                get { return logLevel; }
            }

            public object UserData
            {
                get { return userData; }
                set { userData = value; }
            }
        }

        public class Helptool
        {
            public static T ConvertToType<T>(object value)
            {
                try
                {
                    return (T)Convert.ChangeType(value, typeof(T));
                }
                catch (InvalidCastException)
                {
                    // 转换失败的处理
                    Console.WriteLine($"Conversion to {typeof(T).Name} failed.");
                    return default(T); // 或者抛出异常，具体情况可以根据需求来处理
                }
            }


            public static object ConvertToType(object value, Type targetType)
            {
                try
                {
                    return Convert.ChangeType(value, targetType);
                }
                catch (InvalidCastException)
                {
                    // 转换失败的处理
                    Console.WriteLine($"Conversion to {targetType.Name} failed.");
                    return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
                    // 或者抛出异常，具体情况可以根据需求来处理
                }
            }
        }
    }
}
